package utils

import (
	"strings"
)

//过滤处理没有挂载指定node类型的树节点
func ExcludeNoNodeType(src []map[string]interface{}, nodeType string) []map[string]interface{} {
	//所有分组
	allDeptDataList := make(map[string]map[string]interface{}, 0)
	showNodeList := make(map[string]map[string]interface{}, 0)
	//格式处理之后的分组返回
	formatDataList := make(map[string]map[string]interface{}, 0)
	//数据遍历处理
	for _, srcItem := range src {
		//节点类型
		deptType := GetCommUtil().GetStringValueFiled(srcItem, "type")
		//获取key
		deptId := GetCommUtil().GetStringValueFiled(srcItem, "deptId")
		//当前非分组节点
		if strings.EqualFold(deptType, nodeType) {
			//要显示的节点
			showNodeList[deptId] = srcItem
			//要返回的数据
			formatDataList[deptId] = srcItem
		} else {
			//分组数据
			allDeptDataList[deptId] = srcItem
		}
	}
	//上级节点的递归父类节点
	recursionTree(allDeptDataList, showNodeList, formatDataList)
	//格式处理
	return GetCommUtil().FormatMapToArray(formatDataList)
}

func recursionTree(allData map[string]map[string]interface{}, showNodeList map[string]map[string]interface{}, formatDataList map[string]map[string]interface{}) {
	//遍历
	if len(showNodeList) > 0 {
		//非分组节点的当前父节点
		newShowNodeList := make(map[string]map[string]interface{}, 0)
		//非分组节点的上一次节点
		for _, showNodeItem := range showNodeList {
			//节点类型
			parentId := GetCommUtil().GetStringValueFiled(showNodeItem, "parentId")
			//非分组节点
			if dataItem, ok := allData[parentId]; ok {
				//要显示的节点
				newShowNodeList[parentId] = dataItem
				//要返回的数据
				formatDataList[parentId] = dataItem
			}
		}
		recursionTree(allData, newShowNodeList, formatDataList)
	}
}

// ConvrtListToTree 列表数据转成树
// @str 原始数据列表
// @dataKey 数据主键标识
func ConvrtListToTree(src []map[string]interface{}, dataKey ...string) []map[string]interface{} {
	primaryKey := "id"
	if dataKey != nil {
		primaryKey = dataKey[0]
	}
	// 数据缓存
	allDataList := []map[string]interface{}{}
	rootIdList := make(map[string]string)
	allIdList := make(map[string]string)
	for _, item := range src {
		id := item[primaryKey].(string)
		parentId := item["parentId"].(string)
		//数据缓存
		allDataList = append(allDataList, item)

		//ID缓存
		allIdList[id] = id
		rootIdList[parentId] = parentId
	}

	//剔除无效根节点
	for k := range allIdList {
		if len(rootIdList[k]) != 0 {
			delete(rootIdList, k)
		}
	}

	//填充每个跟节点数据
	treeList := []map[string]interface{}{}
	for k := range rootIdList {
		child := getJsonTree(primaryKey, allDataList, k)
		for _, node := range child {
			treeList = append(treeList, node)
		}
	}

	//机构分组树按创建时间排序
	if len(treeList) > 0 {
		if _, ok := treeList[0]["menuName"]; ok {
			SortMapsByKey(treeList, "orderNum", false)
		} else {
			SortMapsByKey(treeList, "deptName")
		}
	}
	return treeList
}

// ConvrtListToTree 列表数据转成树
// @str 原始数据列表
// @dataKey 数据主键标识
func GetRootList(src []map[string]interface{}, dataKey ...string) []map[string]interface{} {
	primaryKey := "id"
	if dataKey != nil {
		primaryKey = dataKey[0]
	}
	// 数据缓存
	deptMap := make(map[string]map[string]interface{})
	rootIdList := make(map[string]string)
	allIdList := make(map[string]string)
	for _, item := range src {
		id := item[primaryKey].(string)
		parentId := item["parentId"].(string)
		//数据缓存
		deptMap[id] = item

		//ID缓存
		allIdList[id] = id
		rootIdList[parentId] = parentId
	}

	//剔除无效根节点
	for k := range allIdList {
		if len(rootIdList[k]) != 0 {
			delete(rootIdList, k)
		}
	}

	//填充每个跟节点数据
	resList := make([]map[string]interface{}, 0)
	for id := range rootIdList {
		item := deptMap[id]
		if item == nil {
			continue
		}
		item["id"] = item["deptId"]
		item["text"] = item["deptName"]
		item["title"] = item["deptName"]
		resList = append(resList, item)
	}
	//用户无root权限，获取子机构
	if len(resList) == 0 {
		for i := range src {
			item := src[i]
			parentId := item["parentId"].(string)
			if _, ok := rootIdList[parentId]; !ok {
				continue
			}
			if item == nil {
				continue
			}
			item["id"] = item["deptId"]
			item["text"] = item["deptName"]
			item["title"] = item["deptName"]
			resList = append(resList, item)
		}
	}
	if len(resList) > 0 {
		if resList[0]["deptId"] != nil {
			SortMapsByKey(resList, "deptName")
		} else if resList[0]["menuId"] != nil {
			SortMapsByKey(resList, "orderNum", false)
		}
	}
	return resList
}

func getJsonTree(primaryKey string, dataList []map[string]interface{}, pid string) []map[string]interface{} {
	childList := []map[string]interface{}{}
	for _, node := range dataList {
		dataId := node[primaryKey].(string)
		dataPid := node["parentId"].(string)

		//添加当前节点
		if dataPid == pid {
			child := getJsonTree(primaryKey, dataList, dataId)
			if node["menuId"] != nil {
				node["id"] = node["menuId"]
				node["text"] = node["menuName"]
				node["title"] = node["title"]
				node["permission"] = node["perms"]
				//node["key"] = node["menuId"]
			} else if node["deptId"] != nil {
				node["id"] = node["deptId"]
				node["text"] = node["deptName"]
				node["title"] = node["deptName"]
			}
			isParent := len(child) > 0
			if isParent {
				node["children"] = child
			}
			node["hasChildren"] = isParent
			node["leaf"] = !isParent
			childList = append(childList, node)
		}
	}
	//机构分组树按创建时间排序
	if len(childList) > 0 {
		if childList[0]["deptId"] != nil {
			SortMapsByKey(childList, "deptName")
		} else if childList[0]["menuId"] != nil {
			SortMapsByKey(childList, "orderNum", false)
		}
	}
	return childList
}
